package org.ourgrid.peer.ui.async.gui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.ourgrid.common.ui.AbstractInputFieldsPanel;
import org.ourgrid.common.ui.InputFieldsUI;
import org.ourgrid.peer.PeerConfiguration;
import org.ourgrid.peer.ui.async.model.PeerAsyncUIModel;

import br.edu.ufcg.lsd.commune.container.ContainerUtils;

public class VomsAuthPanel extends AbstractInputFieldsPanel implements InputFieldsUI {

    /**
	 * 
	 */
	private static final long serialVersionUID = 7615549557649751185L;


	/** Creates new form VomsAuthPanel */
    public VomsAuthPanel(PeerAsyncUIModel model) {
    	super(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    protected void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        useVomsCheckBox = new javax.swing.JCheckBox();
        vomsURLLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        urlList = new javax.swing.JList();
        addURLButton = new javax.swing.JButton();
        removeURLButton = new javax.swing.JButton();
        urlListModel = new DefaultListModel();
        urlList.setModel(urlListModel);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("VOMS Authorization"));

        useVomsCheckBox.setText("Use VOMS Authorization");
        useVomsCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                useVomsComboBoxActionPerformed(evt);
            }
        });

        removeURLButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeURLActionPerformed();
            }
        });
        
        addURLButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addURLActionPerformed();
            }
        });
        
        vomsURLLabel.setText("Peer VOs addresses:");

        jScrollPane1.setViewportView(urlList);

        addURLButton.setText("Add VO");

        removeURLButton.setText("Remove VO");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(useVomsCheckBox)
                    .add(vomsURLLabel)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 290, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(removeURLButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(addURLButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(useVomsCheckBox)
                .add(12, 12, 12)
                .add(vomsURLLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(addURLButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removeURLButton))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>

    protected void addURLActionPerformed() {
		String urlToAdd = JOptionPane.showInputDialog(this, "VO URL to be added:", "VOMS Authentication", JOptionPane.PLAIN_MESSAGE);
		if (urlToAdd != null && urlToAdd.length() > 0) {
			urlListModel.addElement(urlToAdd);
		}
	}

	protected void removeURLActionPerformed() {
    	for (Object selectedURL : urlList.getSelectedValues()) {
    		urlListModel.removeElement(selectedURL);
    	}
	}

	private void useVomsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
		
    }

    // Variables declaration - do not modify
    private javax.swing.JButton addURLButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton removeURLButton;
    private javax.swing.JList urlList;
    private javax.swing.JCheckBox useVomsCheckBox;
    private javax.swing.JLabel vomsURLLabel;
    private DefaultListModel urlListModel;
    // End of variables declaration
	
    
    public void disableInput() {
		addURLButton.setEnabled(false);
		removeURLButton.setEnabled(false);
		urlList.setEnabled(false);
		useVomsCheckBox.setEnabled(false);
	}

	public void enableInput() {
		addURLButton.setEnabled(true);
		removeURLButton.setEnabled(true);
		urlList.setEnabled(true);
		useVomsCheckBox.setEnabled(true);
		
	}

	public void initFields() throws IOException {
		String useVomsStr = model.getProperty(PeerConfiguration.PROP_USE_VOMS);
		boolean useVoms = ContainerUtils.isEnabled(useVomsStr);
		useVomsCheckBox.setSelected(useVoms);
		
		urlListModel.clear();
		
		String vomsUrlStr = model.getProperty(PeerConfiguration.PROP_VOMS_URL);		
		List<String> vomsUrls = ContainerUtils.parseStringList(vomsUrlStr);
		for (String vomsUrl : vomsUrls) {
			urlListModel.addElement(vomsUrl);
		}
	}

	public void saveFieldInputs() throws IOException {
		model.setProperty(PeerConfiguration.PROP_USE_VOMS, 
    			ContainerUtils.getBooleanProperty(useVomsCheckBox.isSelected()));
		
		List<String> urlList = new ArrayList<String>();
		for (int i = 0; i < urlListModel.getSize(); i++) {
			urlList.add((String) urlListModel.getElementAt(i));
		}
		
		model.setProperty(PeerConfiguration.PROP_VOMS_URL, 
				ContainerUtils.getStringListProperty(urlList));
	}

}

